package chap10collection.test16;

/**
 * @author zdl
 * @date 2024/12/17
 */
import java.util.*;
class Student {
    int age;
    String name;
    public Student(){}
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }
    public int hashCode(){
        return name.hashCode() + age;
    }
    public boolean equals(Object o){
        if (o == null) return false;
        if (o == this) return true;
        if (o.getClass() != this.getClass()) return false;
        Student stu = (Student) o;
        if (stu.name.equals(name) && stu.age == age) return true;
        else return false;
    }
}
public class Demo16 {
    public static void main(String args[]){
        Set set = new HashSet();
        Student stu1 = new Student();
        Student stu2 = new Student("“Tom”", 18);
        Student stu3 = new Student("“Tom”", 18);
        set.add(stu1); //报错  报空指针异常  原因是name没有值 name.hashCode 报空指针
        set.add(stu2);
        set.add(stu3);
        System.out.println(set.size());
    }
}
